USE [DWH_REPORT]
GO

/****** Object:  StoredProcedure [dbo].[BaoCaoGiaiNgan3]    Script Date: 08/04/2012 18:21:41 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[BaoCaoGiaiNgan3]
	
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
    -- Insert statements for procedure here
select distinct LD.[CUSTOMER.ID],LD.[LD.LINK],LD.[LD.ID],LD.[INTEREST.RATE],LD.[REPORT.DATE],LD.TERM
from LD
right join
		(
			select distinct LD.[CUSTOMER.ID],LD.[LD.ID],MAX(LD.[REPORT.DATE]) as maxdate
			from LD
			group by LD.[CUSTOMER.ID],LD.[LD.ID]
		)temp on LD.[CUSTOMER.ID]=temp.[CUSTOMER.ID] and LD.[REPORT.DATE]=temp.maxdate
	
	
END
GO

